package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.pojo.User;
import org.springframework.web.bind.annotation.*;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/6 14:07
 */
@RestController
public class SearchController {
    @RequestMapping("/search")
    @HystrixCommand(fallbackMethod = "testFallBack")
    public String test(){
        System.out.println("这是第一个");
        //int i=1/0;
        return  "这个Search微服务被调用到了!";

    }
    @RequestMapping("/api/search")
    public String apitest(){
        System.out.println("这是第一个");
        //int i=1/0;
        return  "这个Search微服务被调用到了!";

    }

    public String testFallBack(){

        return "错误";
    }


    /**
     * 模拟根据id查找
     * @param id
     * @return
     */
    @GetMapping("/search/{id}")
    @HystrixCommand(fallbackMethod = "findByidFallBack")
    public  Object findByid(@PathVariable Integer id){
        //模拟出错
        //int i=1/0;
        return new User(1,"张三",12,"dada");

    }


    public Object findByidFallBack(Integer id){

        return  new User(-1,"异常",1,"返回异常");
    }

    /**
     * 模拟根据id姓名查找
     * @param id
     * @param name
     * @return
     */
     @GetMapping("/findCustomer")
    public  Object findByIdAndName(@RequestParam Integer id,@RequestParam String name){

        return  new User(2,"王五",13,"dada");
    }

    /**
     * 模拟保存数据
     * @param user
     * @return
     */
    @PostMapping("/save")
    public  Object save(@RequestBody User user){
         return  user;
    }
}
